package fileProcessors.implementations;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

import fileProcessors.FileProcessor;

public class FitSplit extends FileProcessor {
	private BufferedWriter outputWriter;
	private BufferedReader br;

	public FitSplit() {

	}

	public FitSplit(String processButton, String includeFilename, String excludeFilename) {
		super(processButton, includeFilename, excludeFilename);
	}

	@Override
	public void processFiles(File files[], File currentDirectory, File outputFile) {
		try {
			for (int i = 0; i < files.length; i++) {
				br = new BufferedReader(new FileReader(files[i]));
				String line = br.readLine();
				if (line != null) {
					// D lesen
					outputWriter = new BufferedWriter(new FileWriter(new File(currentDirectory, "fit_D.dat")));
					outputWriter.write("c\tD");
					outputWriter.newLine();
					while (line.hashCode() != 0) {
						outputWriter.write(line);
						outputWriter.newLine();
						line = br.readLine();
					}
					outputWriter.close();

					outputWriter = new BufferedWriter(new FileWriter(new File(currentDirectory, "fit_D_fitted.dat")));
					outputWriter.write("c\tD fit");
					outputWriter.newLine();
					line = br.readLine();
					while (line.hashCode() != 0) {
						outputWriter.write(line);
						outputWriter.newLine();
						line = br.readLine();
					}
					outputWriter.close();

					outputWriter = new BufferedWriter(new FileWriter(new File(currentDirectory, "fit_DT.dat")));
					line = br.readLine();
					outputWriter.write("c\tDT");
					outputWriter.newLine();
					while (line.hashCode() != 0) {
						outputWriter.write(line);
						outputWriter.newLine();
						line = br.readLine();
					}
					outputWriter.close();

					outputWriter = new BufferedWriter(new FileWriter(new File(currentDirectory, "fit_DT_fitted.dat")));
					line = br.readLine();
					outputWriter.write("c\tDT fit");
					outputWriter.newLine();
					while (line.hashCode() != 0) {
						outputWriter.write(line);
						outputWriter.newLine();
						line = br.readLine();
					}
					outputWriter.close();

					outputWriter = new BufferedWriter(new FileWriter(new File(currentDirectory, "fit_ST.dat")));
					line = br.readLine();
					outputWriter.write("c\tST");
					outputWriter.newLine();
					while (line.hashCode() != 0) {
						outputWriter.write(line);
						outputWriter.newLine();
						line = br.readLine();
						// System.out.println(hash);
					}
					outputWriter.close();

					outputWriter = new BufferedWriter(new FileWriter(new File(currentDirectory, "fit_ST_fitted.dat")));
					line = br.readLine();
					outputWriter.write("c\tST fit");
					outputWriter.newLine();
					while (line.hashCode() != 0) {
						outputWriter.write(line);
						outputWriter.newLine();
						line = br.readLine();
						if (line == null) {
							break;
						}
					}
					outputWriter.close();
				}
				br.close();

			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public String getTxtProcessBtn() {
		return "split";
	}

	public String getTxtExcludeFilename() {
		return "";
	}

	public String getTxtIncludeFilename() {
		return "fit.dat";
	}

	@Override
	public String getInformation() {
		return "Liest die Datei fit.dat vom Programm fit.c ein und speichert die funktionen und werte von" +
				" diffusions/ thermodiffusions/ soret-Koeffizienten in die dateien fit_*.dat ";
	}

	@Override
	public String getDate() {
		return "10.08.2012";
	}

}
